package com.itheima.device.controller;


import com.github.pagehelper.Page;
import com.itheima.device.pojo.Order;
import com.itheima.device.service.OrderService;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@CrossOrigin//开启跨越请求操作
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 查询全部订单
     */
    @GetMapping("/findAll")
    public Result findAll(){
        try {
            List<Order> orderList = orderService.findAll();
            return  new Result(true, StatusCode.OK,"查询订单成功",orderList);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,StatusCode.ERROR,"查询订单失败");
        }

    }
    /**
     * 根据订单id查询
     * @paramorder
     * @return
     */
    @GetMapping("/order/{id}")
    public  Result findByID (@PathVariable Integer id) {
        Order order = null;
        try {
            Order order1 = orderService.findById(id);
            return new Result(true, StatusCode.OK, "订单查询成功",order1);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "订单查询失败");

        }

    }
    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/findPage/{page}/{size}")
    public  Result  findPage(@PathVariable int page,@PathVariable int size){
        try {
            Page<Order> pageList = orderService.findPage(page, size);
            PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
            return new Result(true,StatusCode.OK,"查询分页成功",pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.OK,"分页查询失败");
        }


    }

    /*
     * 修改订单信息
     * @param device
     * @return
     */
    @PutMapping("/update")
    public Result updateDevice(@RequestBody Order order  ){
        try {
            orderService.update(order);
            return new Result(true,StatusCode.OK,"修改订单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"修改订单失败");
        }


    }
}
